package com.example.enews.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.enews.R
import com.example.enews.adapters.RankAdapter
import com.example.enews.models.RankItem

class DataFragment : Fragment() {

    private lateinit var adapter: RankAdapter
    private lateinit var recyclerView: RecyclerView

    // 新增表头 TextView 引用
    private lateinit var tvColumn1: TextView
    private lateinit var tvColumn2: TextView
    private lateinit var tvColumn3: TextView
    private lateinit var tvColumn4: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_data, container, false)

        recyclerView = view.findViewById(R.id.recyclerView)
        val btnTeams = view.findViewById<Button>(R.id.btn_teams)
        val btnPlayers = view.findViewById<Button>(R.id.btn_players)

        tvColumn1 = view.findViewById(R.id.tvColumn1)
        tvColumn2 = view.findViewById(R.id.tvColumn2)
        tvColumn3 = view.findViewById(R.id.tvColumn3)
        tvColumn4 = view.findViewById(R.id.tvColumn4)

        adapter = RankAdapter()
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        recyclerView.adapter = adapter

        val teamData = listOf(
            RankItem("Vitality", "欧洲", 2088.0, 1),
            RankItem("Spirit", "欧洲", 1926.0, 2),
            RankItem("MOUZ", "欧洲", 1854.0, 3),
            RankItem("MongolZ", "亚洲", 1803.0, 4),
            RankItem("Falcons", "欧洲", 1788.0, 5),
            RankItem("Aurora", "欧洲", 1739.0, 6),
            RankItem("NAVI", "欧洲", 1675.0, 1),
            RankItem("G2", "欧洲", 1670.0, 2),
            RankItem("FaZe", "欧洲", 1654.0, 3),
            RankItem("FURIA", "南美", 1644.0, 4),
            RankItem("GL", "欧洲", 1633.0, 5),
            RankItem("3DMAX", "欧洲", 1624.0, 6)
        )

        val playerData = listOf(
            RankItem("ZywOo", "Vitality",1.33, 1),
            RankItem("donk", "Spirit", 1.30, 1),
            RankItem("sh1ro", "Spirit", 1.26, 1),
            RankItem("s1mple", "FaZe", 1.24, 1),
            RankItem("mONESY", "Falcon", 1.24, 1),
            RankItem("blameF", "Fnatic", 1.24, 1),
            RankItem("deko", "Aurora", 1.23, 1),
            RankItem("nettik", "FlyQuest", 1.22, 1),
            RankItem("gr1ks", "Aurora", 1.22, 1),
            RankItem("DANK1NG", "TYLOO", 1.21, 1),
            RankItem("try", "imperial", 1.20, 1),
            RankItem("KSCERATO", "FURIA", 1.19, 1),
        )

        btnTeams.setOnClickListener {
            updateHeaderForTeams()
            adapter.submitList(teamData)
        }

        btnPlayers.setOnClickListener {
            updateHeaderForPlayers()
            adapter.submitList(playerData)
        }

        updateHeaderForTeams()
        adapter.submitList(teamData)

        return view
    }

    private fun updateHeaderForTeams() {
        tvColumn1.text = "战队"
        tvColumn2.text = "赛区"
        tvColumn3.text = "积分"
        tvColumn4.text = "排名"
    }

    private fun updateHeaderForPlayers() {
        tvColumn1.text = "选手"
        tvColumn2.text = "战队"
        tvColumn3.text = "生涯Rating"
        tvColumn4.text = "排名"
    }
}


